package cn.edu.baiyunu.myapplication.ui.screens.profile

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ChevronRight
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cn.edu.baiyunu.myapplication.ui.components.GlassmorphismCard
import cn.edu.baiyunu.myapplication.ui.components.GradientBackground
import androidx.compose.material.icons.filled.AccountBalance
import androidx.compose.material.icons.filled.Backup
import androidx.compose.material.icons.filled.Category
import androidx.compose.material.icons.filled.Flag
import androidx.compose.material.icons.filled.Help
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Settings
import cn.edu.baiyunu.myapplication.ui.screens.profile.viewmodel.ProfileViewModel

/**
 * 个人资料屏幕
 */
@Composable
fun ProfileScreen(
    navigateToSettings: () -> Unit = {},
    navigateToBudgetManager: () -> Unit = {},
    navigateToDataBackup: () -> Unit = {},
    navigateToSavingsGoal: () -> Unit = {},
    navigateToAbout: () -> Unit = {},
    navigateToFAQ: () -> Unit = {},
    viewModel: ProfileViewModel = hiltViewModel()
) {
    val profileUiState by viewModel.profileUiState.collectAsStateWithLifecycle()
    
    // 分类管理页面状态
    var showCategoryManager by remember { mutableStateOf(false) }
    
    if (showCategoryManager) {
        CategoryManagerScreen(
            onBackClick = { showCategoryManager = false }
        )
    } else {
        GradientBackground {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .systemBarsPadding()
                    .padding(horizontal = 20.dp)
                    .verticalScroll(rememberScrollState())
            ) {
                // 页面标题
                Text(
                    text = "我的",
                    fontSize = 22.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    modifier = Modifier.padding(vertical = 30.dp)
                )
                
                // 用户资料卡片
                GlassmorphismCard(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 25.dp)
                        .clip(RoundedCornerShape(20.dp))
                ) {
                    Row(
                        modifier = Modifier.padding(25.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // 头像
                        Box(
                            modifier = Modifier
                                .size(80.dp)
                                .clip(CircleShape)
                                .background(Color.White.copy(alpha = 0.2f))
                                .border(1.dp, Color.White.copy(alpha = 0.2f), CircleShape),
                            contentAlignment = Alignment.Center
                        ) {
                            Icon(
                                imageVector = Icons.Filled.Person,
                                contentDescription = null,
                                tint = Color.White,
                                modifier = Modifier.size(40.dp)
                            )
                        }
                        
                        // 用户信息
                        Column(
                            modifier = Modifier
                                .padding(start = 20.dp)
                                .weight(1f)
                        ) {
                            // 显示用户名，如果设置中有用户名则显示，否则显示默认值
                            Text(
                                text = profileUiState.settings?.username ?: "用户名",
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color.White
                            )
                            
                            Spacer(modifier = Modifier.height(5.dp))
                            
                            Text(
                                text = "记录每一笔，规划好未来",
                                fontSize = 14.sp,
                                color = Color.White.copy(alpha = 0.7f)
                            )
                        }
                    }
                }
                
                // 功能菜单卡片1
                GlassmorphismCard(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 25.dp)
                        .clip(RoundedCornerShape(20.dp))
                ) {
                    Column {
                        // 分类管理
                        MenuItem(
                            icon = Icons.Filled.Category,
                            text = "分类管理",
                            onClick = { showCategoryManager = true },
                            isLastItem = false
                        )
                        
                        // 储蓄目标管理
                        MenuItem(
                            icon = Icons.Filled.Flag,
                            text = "储蓄目标管理",
                            onClick = navigateToSavingsGoal,
                            isLastItem = false
                        )
                        
                        // 数据备份
                        MenuItem(
                            icon = Icons.Filled.Backup,
                            text = "数据备份",
                            onClick = navigateToDataBackup,
                            isLastItem = false
                        )
                        
                        // 应用设置
                        MenuItem(
                            icon = Icons.Filled.Settings,
                            text = "应用设置",
                            onClick = navigateToSettings,
                            isLastItem = true
                        )
                    }
                }
                
                // 功能菜单卡片2
                GlassmorphismCard(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 25.dp)
                        .clip(RoundedCornerShape(20.dp))
                ) {
                    Column {
                        // 关于应用
                        MenuItem(
                            icon = Icons.Filled.Info,
                            text = "关于应用",
                            onClick = navigateToAbout,
                            isLastItem = false
                        )
                        
                        // 常见问题
                        MenuItem(
                            icon = Icons.Filled.Help,
                            text = "常见问题",
                            onClick = navigateToFAQ,
                            isLastItem = true
                        )
                    }
                }
                
                // 底部空间，确保滚动时底部有足够的空间
                Spacer(modifier = Modifier.height(80.dp))
            }
        }
    }
}

@Composable
private fun MenuItem(
    icon: ImageVector,
    text: String,
    onClick: () -> Unit,
    isLastItem: Boolean
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(18.dp, 20.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 图标背景
        Box(
            modifier = Modifier
                .size(40.dp)
                .clip(RoundedCornerShape(10.dp))
                .background(Color.White.copy(alpha = 0.1f)),
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = Color.White,
                modifier = Modifier.size(24.dp)
            )
        }
        
        // 文本
        Text(
            text = text,
            fontSize = 16.sp,
            color = Color.White,
            modifier = Modifier
                .weight(1f)
                .padding(start = 15.dp)
        )
        
        // 箭头图标
        Icon(
            imageVector = Icons.Filled.ChevronRight,
            contentDescription = null,
            tint = Color.White.copy(alpha = 0.5f),
            modifier = Modifier.size(20.dp)
        )
    }
    
    // 如果不是最后一项，添加分割线
    if (!isLastItem) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(1.dp)
                .padding(horizontal = 16.dp)
                .background(Color.White.copy(alpha = 0.1f))
        )
    }
} 